define(['app','datetime','pagination'], function( app ) {
  'use strict';
  /**
   * 财务管理的退款处理
  */

  app.controller('Caiwu.refund', function ($scope, dataFactoryCRM , $timeout, $q,$compile,$http) {
    $scope.refundPage = {pageAll:1, pageNow:1, pageSize: $scope.psize, k:23};
    $scope.search_param = {status:-1,};
    $scope.confirmData = {};
    $scope.confirmPay = {};
    $scope.detailData = {};
    $scope.refunds = {};

    $scope.enterSearch = function( evt ){
        if (evt && evt.keyCode==13) {
             $scope.search();
        }
    };

    $scope.search = function() {
      $scope.refundPage.pageNow = 1;
      $scope.refundList();
    };

    $scope.payDetail = function( refund_id ) {
        function showDetail(){
            $('.ui.refundDetail.modal').modal({
              autofocus:false
            }).modal('show');
        }
        dataFactoryCRM.getDataByAPI('Money/refundView?id=' + refund_id ).then( function(d) {
            if ( d&&d.rcode==1) {
                $scope.detailData = d.data;
                showDetail();
            }
            else {
                $scope.api_error(d.msg);
            }
        },
        function (err){
            $scope.api_error('接口请求失败');
        }
        );
    };

    $scope.api_error = function ( msg ) {
        $('<div class="ui small modal modal2" style="text-align: center;"><div class="header">提示</div><div class="content"><i class="huge red warning outline icon"></i>' +
            '<p style="padding-top: 15px;">'+msg+'</p>' +'</div></div>'
        ).modal('show');
    };

    $scope.payConfirm = function( refund_id ){
        function showConfirm(){
          $('.ui.payConfirm.modal').find('.form').removeClass("loading");
            $('.ui.payConfirm.modal').modal({
              autofocus:false,
              onApprove:function(){
                  $('input[name="type"]').val(1);
                  if ( $('.payConfirm.form').form('is valid')) {
                      $scope.confirmData.audit = 1;
                      doConfirm();
                  }
                  return false;
              },
              onDeny:function(){
                $('input[name="type"]').val(0);
                if  ($('.payConfirm.form').form('is valid')) {
                    $scope.confirmData.audit = 0;
                    doConfirm();
                }
                return false;
              }
            }).modal('show');
        }

        function doConfirm( ){
            $('.ui.payConfirm.modal').find('.form').addClass("loading");
            var postData = {id:0,pay_name:'',pay_way:'',pay_time:'',audit:0,audit_msg:''};
            postData.id = $scope.confirmData.refund.refund_id;
            postData.audit = $scope.confirmData.audit;
            postData.audit_msg = $scope.confirmData.refund.finance_audit_msg;
            postData.pay_name = $scope.confirmData.refund.pay_name;
            postData.pay_way = $scope.confirmData.refund.pay_way;
            postData.pay_time = $scope.confirmData.refund.pay_time;
            dataFactoryCRM.postDataByAPI('Money/refundConfirm?id='+postData.id, postData)
                        .then(function(d) {
                            if (d&&d.rcode) {
                                var _msg = $scope.confirmData.audit ? '确认打款成功' : '拒绝退款处理成功';
                                $('.ui.payConfirm.modal').modal('hide');
                                $('<div class="ui small modal modal1" style="text-align: center;"><div class="header">提示</div><div class="content"><i class="huge green check circle outline icon"></i>' +
                                        '<p style="padding-top: 15px;">'+_msg+'</p>' +'</div></div>'
                                    ).modal('show');
                                    $timeout(function(){
                                        $scope.refundList();
                                    }, 5);
                                    return true;
                            }
                            else{
                                $scope.api_error(d.msg);
                                return false;
                            }
                        },function( err ){
                            $scope.api_error("接口请求错误");
                        });
        }

        dataFactoryCRM.getDataByAPI('Money/refundConfirm?id=' + refund_id ).then( function(d) {
            if ( d&&d.rcode==1) {
                $scope.confirmData = d.data;
                showConfirm();
            }
            else {
                $scope.api_error(d.msg);
            }
        },
        function (err){
            $scope.api_error('接口请求失败');
        }
        );
    };

    $scope.refundList = function() {
          var sUrl = "Money/refund";
          var sParam = $scope.search_param;
          sParam.psize = $scope.refundPage.pageSize;
          sParam.p = $scope.refundPage.pageNow;
          sUrl += '?' + $.param( sParam );
          dataFactoryCRM.getDataByAPI( sUrl ).then( function( _d ){
          if  (_d.rcode == 1) {
              $scope.refundPage.pageSize = _d.psize;
              $scope.refundPage.pageAll = _d.pall;
              $scope.refundPage.pageNow = _d.p;
              $scope.refunds = _d.data;
            }
            else {
              $scope.refundPage.pageAll = 0;
          }
          });
      };
    //初始化页面显示数据（如果是新弹出独立窗口则从父窗口获取）
    window.loadClientCacheToScope($scope, function() {
         $scope.refundList();
    });

   

    $scope.$watch('search_param.status', function(nV, oV){
       if( nV == oV ) return;
       $scope.search();
    });
  })
  .directive('refundStatus', function() {
            return {
                restrict: 'A',
                template: "{{status}}",
                link: function(scope, element, attrs) {
                    if (attrs.status) {
                        var code = scope.$eval(attrs.status);
                        code = 'A' + code;
                        var Names = {'A0':'待处理','A10':'已取消','A20':'申请失败', 'A30':'风控确认','A31':'退款失败','A40':'退款成功'};
                        if ( code in Names )
                          scope.status = Names[code];
                        else
                          scope.status = '未知';
                    }
                }
            };
        })
});
